<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span style="font-size: 14px">重置密码</span>
    </div>
    <!-- 表单 -->
    <el-form :model="pwdForm" :rules="pwdFormRules" ref="pwdFormRef" label-width="100px">
      <el-form-item label="原密码" prop="old_pwd">
        <el-input v-model="pwdForm.old_pwd" type="password"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="new_pwd">
        <el-input v-model="pwdForm.new_pwd" type="password"></el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="re_pwd">
        <el-input v-model="pwdForm.re_pwd" type="password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="updatePwd">修改密码</el-button>
        <el-button @click="resetFn">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import user from '@/api/user'
import { mapMutations } from 'vuex'

export default {
  name: 'UserPwd',
  components: {},
  data () {
    const notSameOldPwd = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入新密码'))
      } else if (value === this.pwdForm.old_pwd) {
        callback(new Error('新密码不能与原密码相同!'))
      } else {
        callback()
      }
    }
    const samePwd = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入旧密码'))
      } else if (value !== this.pwdForm.new_pwd) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      // 表单的数据对象
      pwdForm: {
        old_pwd: '',
        new_pwd: '',
        re_pwd: ''
      },
      // 表单的验证规则对象
      pwdFormRules: {
        old_pwd: [
          { required: true, message: '请输入旧密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码长度必须是6-15位的非空字符串', trigger: 'blur' }
        ],
        new_pwd: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码长度必须是6-15位的非空字符串', trigger: 'blur' },
          { validator: notSameOldPwd, trigger: 'blur' }
        ],
        re_pwd: [
          { required: true, message: '请再次输入新密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码长度必须是6-15位的非空字符串', trigger: 'blur' },
          { validator: samePwd, trigger: 'blur' }
        ]
      }
    }
  },
  computed: {},
  watch: {},
  methods: {
    ...mapMutations(['UpdateToken', 'UpdateUserInfo']),
    // 1.重置表单
    resetFn () {
      this.$refs.pwdFormRef.resetFields()
    },
    // 2.确认修改密码
    updatePwd () {
      this.$refs.pwdFormRef.validate(async valid => {
        if (valid) {
          const { data: res } = await user.updateUserPwd(this.pwdForm)
          if (res.code === 0) {
            // 强制登录
            this.UpdateToken('')
            this.UpdateUserInfo({})
            this.$router.push('/login')
            this.$message.success(res.message)
          } else {
            this.$message.error(res.message)
          }
        } else {
          return false
        }
      })
    }
  },
  created () {

  },
  mounted () {

  }
}
</script>
<style scoped>
.el-form {
  width: 40%;
}
</style>
